package main

import (
	"flag"
	"fmt"
	"os"
	"toren/encrypt/pkg"
)

var (
	isEncrypt        bool
	isDecrypt        bool
	isGenerateAESKey bool
	key              string
	value            string
	keyType          string
)

func initFlag() {
	flag.BoolVar(&isEncrypt, "e", false, "加密")
	flag.BoolVar(&isDecrypt, "d", false, "解密")
	flag.BoolVar(&isGenerateAESKey, "g", false, "是否生成随机密钥")
	flag.StringVar(&key, "k", "", "加解密时需要的key")
	flag.StringVar(&value, "v", "", "需要加密或解密的值")
	flag.StringVar(&keyType, "t", "", "加解密类型")
	flag.Parse()
}
func main() {
	initFlag()
	var encryptor pkg.Encryptor
	if keyType == "sm4" {
		encryptor = pkg.SM4Encryptor{}
	} else if keyType == "aes" {
		encryptor = pkg.AESEncryptor{}
	}
	if isGenerateAESKey {
		fmt.Println(fmt.Sprintf("Generate rand key:%s", encryptor.RandKey()))
		os.Exit(0)
	}
	if isEncrypt {
		fmt.Println(fmt.Sprintf("key:%s,value:%s", key, value))
		encryptValue, err := encryptor.Encrypt(key, value)
		if err != nil {
			panic(err)
		}
		fmt.Println(fmt.Sprintf("Encrypt value:%s", string(encryptValue)))
	}
	if isDecrypt {
		decryptValue, err := encryptor.Decrypt(key, value)
		if err != nil {
			panic(err)
		}
		fmt.Println(fmt.Sprintf("Decrypt value:%s", string(decryptValue)))
	}
}
